const CONFIG = require('../../../config.js');
const util = require('../../../utils/util.js')
const AREAARR = require('../../../utils/areaList.js')
let currentDay = util.formatDate(new Date());
let TEXTData = {};

Page({
	data: {
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#39a9ed))',
		loginInfo:null,
		areaArr: AREAARR,
		isList: [ {name: '是', id: '1'}, {name: '否', id: '0'} ],
		E002List: [ {name: '1-3次', id: '1'}, {name: '4-6次', id: '2'}, {name: '7-9次', id: '3'}, {name: '10-15次', id: '4'}, {name: '15次以上', id: '5'} ],
		E009List: [ {name: '无查询', id: '1'}, {name: '1-3次', id: '2'}, {name: '4-6次', id: '3'}, {name: '7-10次', id: '4'}, {name: '11-15次', id: '5'}, {name: '15次以上', id: '6'} ],
		E013List: [],
		E017List: [{name: 'N', id: '0'},{name: '1', id: '1'},{name: '2', id: '2'},{name: '3', id: '3'},{name: '4', id: '4'},{name: '5', id: '5'},{name: '6', id: '6'},{name: '7', id: '7'}],
		formData:{
			E001: '',
			E002: '',
			E003: '',
			E004: '',
			E005: '',
			E006: '',
			E007: '',
			E008: '',
			E009: '',
			E010: '',
			E011: '',
			E012: '',
			E013: [],
			E014: '',
			E015: '',
			E016: '',
            E017: '',
            E018: '',
            E019: '',
            E020: '',
		},
		currentDate: currentDay,
		areaValue: '',
		cityList: [],
		areaList: [],
		productTypeid: '',
		proId: '',
		TEXTDataList: [],
	},
	// 城市选择
	bindChange: function (e){
		let provinceCode = this.data.areaArr.provinceList[e.detail.value[0]].code;
		this.setData({
			cityList: this.data.areaArr.cityList[provinceCode]
		})
	},

	// 时间选择
	bindDateChange: function (e) {
		let {field} = e.currentTarget.dataset;
		this.setData({
			applyTime: e.detail.value,
			[`formData.${field}`]: e.detail.value
		})
	},

	// 失去焦点事件
	blurFn(e){
		if(e.currentTarget.dataset.field === "E016"){
			if(Number(this.data.formData.E014) + Number(this.data.formData.E015) > Number(this.data.formData.E016)){
				wx.showModal({
					title: '',
					content: '未结清贷款总机构数不能大于 未结清小贷机构数 和 未结清经营性贷款机构数 之和！',
					showCancel: false,
				})
			}
		}
	},

  	// 单选
	  radioChange: function (e) {
		const {field} = e.currentTarget.dataset;
		let val = e.currentTarget.dataset.value;
		let itemData = this.data.formData;
		console.log(val, itemData.E002,1002)
		if(field === 'E002'){
			this.setData({
				[`formData.E004`]: itemData.E004 > val ? val : itemData.E004,
				[`formData.E006`]: itemData.E006 > val ? val : itemData.E006,
				[`formData.E008`]: itemData.E008 > val ? val : itemData.E008,
			})
		}else if(field === 'E004'){
			this.setData({
				[`formData.E006`]: itemData.E006 > val ? val : itemData.E006,
				[`formData.E008`]: itemData.E008 > val ? val : itemData.E008,
			})
		}else if(field === 'E006'){
			this.setData({
				[`formData.E008`]: itemData.E008 > val ? val : itemData.E008,
			})
		}else if(field === 'E017'){
			this.setData({
				[`formData.E018`]: itemData.E018 > val ? val : itemData.E018,
				[`formData.E019`]: itemData.E019 > val ? val : itemData.E019,
				[`formData.E020`]: itemData.E020 > val ? val : itemData.E020,
			})
		}else if(field === 'E018'){
			this.setData({
				[`formData.E019`]: itemData.E019 > val ? val : itemData.E019,
				[`formData.E020`]: itemData.E020 > val ? val : itemData.E020,
			})
		}else if(field === 'E019'){
			this.setData({
				[`formData.E020`]: itemData.E020 > val ? val : itemData.E020,
			})
		}
		this.setData({
			[`formData.${field}`]: val
		})
	},
	// 多选
	checkChange: function (e) {
		const {field} = e.currentTarget.dataset;
		let val = e.currentTarget.dataset.value;
		// 以上皆无 
		if((field === 'E013' && val == '6')){
			this.setData({
				[`formData.${field}`]: [val]
			})
		}else{
            if(this.data.formData.E013[0] === '6'){
                this.data.formData.E013 = [];
            }

			let idx = this.data.formData[field].indexOf(val);
			if(idx > -1){
				this.data.formData[field].splice(idx, 1);
			}else{
				this.data.formData[field].push(val);
			}
			this.setData({
				[`formData.${field}`]: this.data.formData[field]
			})
		}
	},
	// 输入框
	formInputChange(e) {
		const {field} = e.currentTarget.dataset
		this.setData({
			[`formData.${field}`]: e.detail.value
		})
	},

	// 数据回显
	getDataFn: function(){
		let that = this;
		wx.request({
			url: CONFIG.host + "/system/appInfoController/getCustomerProductDetailInfo",
			data: {
				itemType: 'E',
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					let data = res.data.data;
					for(let i = 0; i < data.length; i++){
						if(data[i].itemCode === 'E013'){
							if(data[i].itemValue === null){
								that.data.formData[data[i].itemCode] = [];
							}else{
								that.data.formData[data[i].itemCode] = data[i].itemValue.split(',');
							}
						}else{
							that.data.formData[data[i].itemCode] = data[i].itemValue;
						}
					}
					this.setData({
						formData: that.data.formData
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 提交保存
	submitFn: function(){
		let datas = JSON.parse(JSON.stringify(this.data.formData));
		if(Number(this.data.formData.E014) + Number(this.data.formData.E015) > Number(this.data.formData.E016)){
			wx.showModal({
				title: '',
				content: '未结清贷款总机构数不能大于 未结清小贷机构数 和 未结清经营性贷款机构数 之和！',
				showCancel: false,
			})
			return
		}
		// 提交验证
		let isBool = datas.E001 === '';
		if(isBool){
			wx.showToast({
				title: '请填写完整！',
				icon: 'none',
			})
			return;
		}
		let productInfos = [];
		for(let key in datas){
			if(Array.isArray(datas[key])){
				datas[key] = datas[key].join(',');
			}
			let obj = {
				"itemCode": key,
				"itemValue": datas[key],
				"provinceCode": "",
				"cityCode": "",
				"areaCode": ""
			}
			productInfos.push(obj);
		}
		wx.request({
			url: CONFIG.host + "/system/appInfoController/addCustomerProductInfo",
			data: {
				"productItem":"E",
				"productInfos": productInfos
			},
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.showToast({
					title: res.data.message,
					icon: 'none',
				})
				if(res.data.code === 'success'){
					wx.redirectTo({
						url: '../../productList/productList'
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

  	onLoad: function (options) {
			let showNum = wx.getStorageSync('showUser') === '0' ? '0' : '1';
			TEXTData = require('../../../utils/text'+ showNum +'.js');
			TEXTData = TEXTData.textJson;
			let loginInfo = wx.getStorageSync('loginInfo') || '';
			if(loginInfo){
				this.setData({
					loginInfo : JSON.parse(loginInfo),
					proId: options.proId,
					productTypeid: options.productTypeid,
					TEXTDataList: TEXTData,
					E013List: [{name: TEXTData.yqL, id: '1'}, {name: TEXTData.dzL, id: '2'}, {name: '转出', id: '3'}, {name: '止付', id: '4'}, {name: '关注', id: '5'}, {name: '以上皆无', id: '6'}]
				})
				this.getDataFn();
			}
	  },
	  
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},
})
